<script>
$(document).ready(function () {
  var table = table_main("#node_table");
  $("#all").click(function () { //select all button
    select_all();
  });
  var pageselect = localStorage.getItem('page-select') || '30';
  $("#pageselect").val(pageselect);
  $("#batch-spam").bind('click', function (e) { //batch spam
    batch_nav("batch_spam");
  });
  $("#batch-publish").bind('click', function (e) { //batch publish
    batch_nav("batch_publish");
  });
  $("#batch-ban").bind('click', function (e) { //batch ban
    batch_nav("batch_ban");
  });
  $("#batch-unban").bind('click', function (e) { //batch unban
    batch_nav("batch_unban");
  });
  $("#delete-batch").bind('click', function (e) { // batch delete
    batch_nav("batch_delete");
  });
  $('#pageselect').change(function () {
    pagination("#pageselect", "/spam2/filter/");
  });
  $('#spammed').on('click', function () { //spam filter
    search_table("spammed", "/spam2/filter/");
  });
  $('#unmoderated').on('click', function () { // unmoderated filter
    search_table("unmoderated", "/spam2/filter/");
  });
  $('#published').on('click', function () { // published filter
    search_table("published", "/spam2/filter/");
  });
  $('#reset_all').on('click', function () { // reset all filter
    search_table("all", "/spam2/filter/");
  });
  $('#created').on('click', function () { // Created_at filter
    search_table("created", "/spam2/filter/");
  });
});
    
</script>
<div class="card" id="table-card">
  <div class="bg-light navbar navbar-expand">
    <ul class="nav navbar-expand navbar-nav-scroll">
      <li class="nav-item">
        <a class="btn nav-link text-secondary" data-toggle="tooltip" data-placement="top" title="Selected per page"> Selected <span id="select-count" class="badge badge-dark">0</span></a>
      </li>
      <li class="nav-item">
        <a id="reset_all" class="btn nav-link  <% if params[:type] == "all" %> active<% else %> text-secondary<% end %>"><i class="fa fa-arrow-circle-o-up <% if params[:type] == "all" %> text-dark<% else %> text-secondary<% end %>" data-toggle="tooltip" data-placement="top" title="Latest updated Nodes"></i> New Activity </a>
      </li>
      <% if params[:type] != "wiki" %>
      <li class="nav-item">
        <a id="created" class="btn nav-link  <% if params[:type] == "created" %> active <% else %> text-secondary <% end %>"><i class="fa fa-list-ol <% if params[:type] == "created" %> text-dark<% else %> text-secondary<% end %>" data-toggle="tooltip" data-placement="top" title="Order as per their creation time "></i> Recent </a>
      </li>
      <li class="nav-item">
        <a id="spammed" class="btn nav-link  <% if params[:type] == "spammed" %> active <% else %> text-secondary <% end %>"> <i class="fa fa-ban <% if params[:type] == "spammed" %> text-dark<% else %> text-secondary<% end %>" data-toggle="tooltip" data-placement="top" title="Filter all Spammed Nodes "></i> Spammed <span class="badge badge-dark"><%= Node.where(status: 0).length %></span></a> 
      </li>
      <li class="nav-item">
        <a id="unmoderated" class="btn nav-link  <% if params[:type] == "unmoderated" %> active <% else %> text-secondary <% end %>"> <i class="fa fa-exclamation-circle <% if params[:type] == "unmoderated" %> text-dark<% else %> text-secondary<% end %>" data-toggle="tooltip" data-placement="top" title="Filter all unmoderated Nodes"></i> Unmoderated</a>
      </li>
      <li class="nav-item">
        <a id="published" class="btn nav-link  <% if params[:type] == "published" %> active <% else %> text-secondary <% end %>"> <i class="fa fa-check-circle <% if params[:type] == "published" %> text-dark<% else %> text-secondary<% end %>" data-toggle="tooltip" data-placement="top" title="Filter all Published Nodes"></i> Published</a>
      </li>
      <% end %>
    </ul>
  </div>
  <div class="card-body" style="overflow-x:hidden;">
    <% unless @nodes.empty? %>
    <div class="my-0 text-secondary small "><%= page_entries_info(@nodes) %>s</div>
    <% end %>
    <table  id="node_table" class="nowrap table table-hover" style="width:100%; text-align:left">
      <thead style="text-align:left;">
        <tr>
          <th><input type="checkbox"  id="selectall"/></th>
          <th><%=translation('users.profile.title')%></th>
          <th>Author</th>
          <th>Status</th>
          <th><% if params[:type] == "created"%>Created at <% else %> Updated at <% end %></th>
          <th>Action</th>                                
        </tr>       
      </thead>
      <tbody> 
        <% @nodes.each do |node| %>
          <tr id="n<%= node.id %>">
            <td><input  class="selectedId" value="<%= node.nid %>" type="checkbox" /></td>
            <td>
              <i class="fa fa-file text-secondary"></i> 
              <a href="javascript:void(0);" class="text-dark font-weight-bold" id="node-hover" data-toggle="modal" data-target="#ninfo<%= node.id %>"><%= node.title.truncate(15) %></a><br />
               <span class="text-secondary small"><%= node.type.capitalize %> | <% if params[:type] == "created"%><%= time_ago_in_words(node.updated_at) %><% else %><%= time_ago_in_words(node.created_at) %><% end %> ago</span>
            </td>
            <td>  
              <a href="/profile/<%= node.author&.name %>" class="text-info"><%= node.author&.name.truncate(15) %> </a><br>
               <span class="text-secondary small"><%= Node.where(uid: node.author&.id).count %> Nodes<span> <%= node.author&.new_contributor%>
             </td>
            <td>
              <% if node.status == 1 %><span class="badge badge-success"><i class="fa fa-check-circle text-white"></i> Published</span><% elsif node.status == 0 %><span class="badge badge-danger"><i class="fa fa-ban text-white"></i> Spammed</span><% elsif node.status == 4 %><span class="badge badge-primary"><i class="fa fa-circle text-white"></i> Unmoderated</span><% end %>
            </td>
             <td>
               <span class="text-dark"><% if params[:type] == "created" %><%= time_ago_in_words(node.created_at) %> <% else %><%= time_ago_in_words(node.updated_at) %> <% end %> ago</span>
            </td>
             <td>
               <a class="btn btn-xs border-curve font-weight-bold btn<% if node.status != 1 %>-success<% else %>-secondary disabled<% end %> publish" data-remote="true" href="/moderate/publish/<%= node.id %>" ><i class="fa fa-check-circle fa-white"></i> Publish post</a>
               <a class="btn btn-xs border-curve font-weight-bold btn<% if node.status != 0 %>-danger<% else %>-secondary disabled<% end %> spam" data-remote="true" href="/moderate/spam/<%= node.id %>"><i class="fa fa-ban fa-white"></i> Spam post</a> 
               <a class="btn btn-xs border-curve font-weight-bold btn-secondary ban a<%= node.author.id %>" <% if node.author.status == 0 %>style="display:none;"<% end %> data-remote="true" href="/ban/<%= node.author.id %>">Ban user</a>
               <a class="btn btn-xs border-curve font-weight-bold btn-secondary unban a-unban<%= node.author.id %>" <% if node.author.status == 1 %>style="display:none;"<% end %> data-remote="true" href="/unban/<%= node.author.id %>">Unban user</a>
               <%= link_to "/notes/delete/#{node.id}", data: { confirm: "Are you sure you want to delete #{node.path}?" }, :remote => true, :class => "btn border-curve btn-sm font-weight-bold btn-light delete" do %>
                <i class="fa fa-trash text-dark"></i>
               <% end %>
               <script>
                $('#n<%= node.id %> .delete').bind('ajax:success', function(e){
                  $('#n<%= node.id %>').fadeOut()
                  notyNotification('relax', 3000, 'danger', 'topRight', 'Node deleted');
                });
                $('#n<%= node.id %> .publish').bind('ajax:success', function(e){
                  $('#n<%= node.id %>').hide()
                  notyNotification('relax', 3000, 'success', 'topRight', 'Node published');

                });
                $('#n<%= node.id %> .spam').bind('ajax:success', function(e){
                  $('#n<%= node.id %>').hide()
                  notyNotification('relax', 3000, 'danger', 'topRight', 'Node spammed');

                });  
                $('.a<%= node.author.id %>.ban').bind('ajax:success', function(e){
                  $('.a<%= node.author.id %>').hide()  // ban toggle
                  $('.a-unban<%= node.author.id %>').show()
                  notyNotification('relax', 3000, 'danger', 'topRight', 'Author banned');

                });
                $('.a-unban<%= node.author.id %>.unban').bind('ajax:success', function(e){
                  $('.a-unban<%= node.author.id %>').hide()
                  $('.a<%= node.author.id %>').show()
                  notyNotification('relax', 3000, 'success', 'topRight', 'Author unbanned');
                });
                </script>
              </td>
            </tr>
            <div class="modal fade" id="ninfo<%= node.id %>"> 
              <div class="modal-dialog" >
                <div class="modal-content">
                  <div class="modal-header">
                    <h5 class="modal-title"><a href="<%= node.path %>"><%= node.title %></a></h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                      <span>&times;</span>
                    </button>
                  </div>
                  <div class="modal-body">
                    <% if node.tags.length>0 %>
                      <div class="mb-2">
                        <% node.tags.limit(4).each do |tag| %>
                          <%= link_to tag.name, tag_path(tag.name), class: 'badge badge-primary' %>
                        <% end %>
                      </div>
                    <% end %>
                    <%= node.body %>
                  </div>
                  <div class="modal-footer">
                    <%= time_ago_in_words(node.created_at) %> ago
                  </div>
                </div>
              </div>
            </div>
          <% end %>
        </tbody>
      </table>                       
    </div>
  </div>
</div>
<div class="page-table">
  <div class="float-right">
    <%= will_paginate @nodes, :renderer => WillPaginate::ActionView::BootstrapLinkRenderer unless @unpaginated || @nodes.empty? %>
  </div>
  <div class="float-left text-secondary">
    <% unless @nodes.empty? %> Page number <%= @nodes.current_page %> of <%= @nodes.total_pages %> total Pages <% end %>
  </div>
</div>
